package com.apps.order.service.impl;

import com.apps.globals.activity.enums.ActivityType;
import com.apps.globals.activity.model.po.KupaActivity;
import com.apps.globals.activity.service.IKupaActivityService;
import com.apps.member.account.enums.AccountItemMinType;
import com.apps.member.account.service.IKupaAccountService;
import com.apps.order.enums.OrderStatus;
import com.apps.order.enums.PayType;
import com.apps.order.model.po.KupaMemberRechargeOrder;
import com.apps.order.dao.KupaMemberRechargeOrderMapper;
import com.apps.order.service.IKupaMemberRechargeOrderService;
import com.apps.order.utils.OrderUtils;
import com.apps.sannotation.constraints.DecimalMaxLen;
import com.apps.sannotation.keys.JoinKey;
import com.apps.saspect.ThreadLock;
import com.apps.saspect.ValidMethod;
import com.apps.sexctption.DataException;
import com.apps.sexctption.NullException;
import com.apps.sworld.dao.impl.sDaoImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大鹏
 * @since 2020-10-02
 */
@Service
public class KupaMemberRechargeOrderServiceImpl extends sDaoImpl<KupaMemberRechargeOrderMapper, KupaMemberRechargeOrder> implements IKupaMemberRechargeOrderService
{

    @Autowired
    private IKupaActivityService activityService;

    @Autowired
    private IKupaAccountService accountService;

    /**
     * 订单前缀
     */
    private String SnPrefix="recharge_";

    @ValidMethod
    @Override
    public KupaMemberRechargeOrder CreateOrder(Integer member_id,@DecimalMaxLen Float price)
    {
        KupaActivity activity=activityService.getOne(ActivityType.RECHARGE,new BigDecimal(price));
        KupaMemberRechargeOrder rechargeOrder=new KupaMemberRechargeOrder();
        rechargeOrder.setMember_id(member_id);
        rechargeOrder.setReal_price(new BigDecimal(price));
        rechargeOrder.setOrder_status(OrderStatus.WAITPAY.name());
        rechargeOrder.setSn(SnPrefix+OrderUtils.TransactionNo());
        rechargeOrder.setAmount_price(null != activity?activity.getAmount_price():new BigDecimal(0));
        rechargeOrder.setActivity_id(null != activity?activity.getId():null);
        rechargeOrder.setPay_price(rechargeOrder.getReal_price().subtract(activity.getAmount_price()));
        return saveToDB(rechargeOrder);
    }

    @ThreadLock
    @Override
    public void PaySuccess(String trade_sn, @JoinKey String sn, BigDecimal price, PayType payType)
    {
        if(!sn.contains(SnPrefix))
            return;
        KupaMemberRechargeOrder rechargeOrder=getOneByFieldValue("sn",sn);
        if(null ==  rechargeOrder)
            throw new NullException("订单"+sn+"不存在");
        if(!rechargeOrder.getOrder_status().equals(OrderStatus.WAITPAY.name()))
            throw new NullException("订单"+sn+"不是待付款状态");
        if(price.compareTo(rechargeOrder.getPay_price()) != 0)
            throw new DataException("订单"+sn+"付款金额和订单金额不一致");
        rechargeOrder.setTrade_sn(trade_sn);
        rechargeOrder.setOrder_status(OrderStatus.PAYSUCESS.name());
        rechargeOrder.setPay_type(payType.name());
        updateById(rechargeOrder);
        //打入账户
        accountService.Income(rechargeOrder.getMember_id(),price, AccountItemMinType.RECHARGE);
    }
}
